package messaging;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;

/**
 * Message-Driven Bean implementation class for: ConsumerMDB
 */
@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue") }, mappedName = "jms/resourceManagerQueue")
public class ConsumerMDB implements MessageListener {

	MessageContainerRemote mc = null;

	/**
	 * Default constructor.
	 */
	public ConsumerMDB() {
		try {
			InitialContext ic = new InitialContext();
			mc = (MessageContainerRemote) ic
					.lookup("java:global/ResourceManagerEAR/ResourceManagerEJB/MessageContainer!messaging.MessageContainerRemote");
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see MessageListener#onMessage(Message)
	 */
	public void onMessage(Message message) {
		TextMessage textMessage = (TextMessage) message;
		try {
			System.out.println("am receptionat urmatorul mesaj: ");
			System.out.println(textMessage.getText());
			System.out.println();
			mc.addMessage(textMessage.getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
	}

}
